package controllers;

import play.*;
import play.db.jpa.GenericModel.JPAQuery;
import play.db.jpa.JPA;
import play.mvc.*;

import java.io.File;
import java.io.IOException;
import java.util.*;

import javax.persistence.Query;

import com.ainibaby6.domain.IConstants;
import com.ainibaby6.domain.models.ShopItem;
import com.ainibaby6.utils.StringTool;

public class Application extends Controller
{

    private static List<ShopItem> queryTopItems(String type, int count)
    {
        Query q = JPA.em().createQuery(
                "from ShopItem where type = ? order by createTime desc");
        q.setParameter(1, type);
        q.setMaxResults(count);
        return q.getResultList();
    }

    public static void index(String type) throws IOException
    {
        if ("girl".equals(type) || !StringTool.isEquals(type, "boy"))
        {
            List<ShopItem> grilItems = queryTopItems(IConstants.GIRL_SHOP_ITEM,
                    10);
            renderArgs.put("girlItems", grilItems);
        }
        if ("boy".equals(type) || !StringTool.isEquals(type, "girl"))
        {
            List<ShopItem> boyItems = queryTopItems(IConstants.BOY_SHOP_ITEM,
                    10);
            renderArgs.put("boyItems", boyItems);
        }
        renderArgs.put("type", type);
        render();
    }

}
